#define  _CRT_SECURE_NO_WARNINGS
#include "stack.h"

void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity*sizeof(STDataType));
		assert(tmp);
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}

	ps->arr[ps->top] = x;
	ps->top++;
}

bool BoolEmpty(ST* ps)
{
	assert(ps);
	
	return ps->top == 0;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(!BoolEmpty(ps));

	ps->top--;
}

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(!BoolEmpty(ps));
	
	return ps->arr[ps->top - 1];
}


int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

void STDestroy(ST* ps)
{
	assert(ps);
	
	ps->capacity = ps->top = 0;
	if(ps->arr)
		free(ps->arr);
	ps->arr = NULL;
}
